package org.opens.text;

import cn.hutool.core.util.StrUtil;
import org.opens.text.config.ParseConfig;
import org.opens.text.config.ParseFromFileConfig;
import org.opens.text.constant.ParseType;
import org.opens.text.parseHandle.ParseHandleFactory;
import org.opens.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 将
 *
 * @author by zhangyuming
 * @date 2022/9/20
 */
public class ParseUtil {

    private static Logger LOGGER = LoggerFactory.getLogger(ParseUtil.class);

    public static void parseStart(ParseConfig parseConfig) {
        parseStartValid(parseConfig);

        parseAutoCalc(parseConfig);

        parseStartBefore(parseConfig);

        parseStartProcessed(parseConfig);

        parseStartAfter(parseConfig);
    }

    private static void parseStartBefore(ParseConfig parseConfig) {

    }

    private static void parseStartProcessed(ParseConfig parseConfig) {
        ParseHandleFactory.getInstance(parseConfig.getParseType())
                .parse(parseConfig);
    }

    private static void parseStartAfter(ParseConfig parseConfig) {
        if (parseConfig.isGenEndOpenGenPath()) {
            FileUtil.openFolder(parseConfig.getGenPath());
        }
    }

    private static void parseAutoCalc(ParseConfig parseConfig) {

    }

    private static void parseStartValid(ParseConfig parseConfig) {
        if (parseConfig == null) {
            throw new IllegalArgumentException("解析配置参数不能为空!");
        }

        if (parseConfig.getParseType() == null) {
            throw new IllegalArgumentException("解析配置参数中的[parseType]不能为空!");
        }

        if (StrUtil.isBlank(parseConfig.getGenPath())) {
            throw new IllegalArgumentException("解析配置参数中的[genPath]不能为空!");
        }

        if (parseConfig.getDbType() == null) {
            throw new IllegalArgumentException("解析配置参数中的[dbType]不能为空!");
        }

        if (ParseType.FILE.equals(parseConfig.getParseType())) {
            ParseFromFileConfig parseFromFileConfig = parseConfig.getParseFromFileConfig();
            if (parseFromFileConfig == null) {
                throw new IllegalArgumentException("从文件解析时, 解析配置参数中的[parseFromFileConfig]不能为空!");
            }
            if (StrUtil.isEmpty(parseFromFileConfig.getSourceFilePath())) {
                throw new IllegalArgumentException("从文件解析时, 解析配置参数中的[parseFromFileConfig][sourceFilePath]不能为空!");
            }
        }
    }

}
